package com.simtop.util.Cqutil;


import com.alibaba.fastjson.JSONObject;

import com.simtop.common.CqKey;
import com.simtop.pojo.qcpo.CqSport;
import com.simtop.util.emailutil.CipherUtil;
import com.simtop.util.httputil.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

/**
* @Author: liuzhaobo
* @Description:  重庆高校  对接
* @DateTime: 2022/10/11 0011 13:12
* @Params: 
* @Return 
*/
@Configuration
public class CqUtil {

    /**
     * 获取access_token   正式上线使用正式地址
     */
    private String ACCESS_TOKEN = "http://test.vsp.cqooc.com/open/api/v2/oauth/token";
    /*private String ACCESS_TOKEN = "http://vsp.cqooc.com/open/api/v2/oauth/token";*/
    /**
     * 提交成绩
     */
    private String SUBMIT_GRADE = "http://test.vsp.cqooc.com/open/api/v2/upload/data";

    /*private String SUBMIT_GRADE = "http://vsp.cqooc.com/open/api/v2/upload/data";*/

    private Logger log = LoggerFactory.getLogger(CqUtil.class);

    /**
     * 获取access_token
     *
     * @param ticket
     * @return json字符串
     */
    public String getAccessToken(String ticket) throws UnsupportedEncodingException {
        //ticket解密
        String rTicket = URLDecoder.decode(ticket, "UTF-8");
        //获取signature  （按照要求进行MD5加密，这是自己定义的工具类方法）
        String signature = CipherUtil.generatePassword(CqKey.issuerId + CqKey.SecretKey + rTicket);
        String signatures = signature.toUpperCase();
        //发送请求获取access_key
        // String url = ACCESS_TOKEN + "?ticket=" + ticket + "&appid=" + CqKey.issuerId + "&signature="+signature;
        String url = ACCESS_TOKEN + "?appId=" + CqKey.issuerId + "&signature=" + signatures + "&ticket=" + ticket;
        String result = "";
        try {
            result = HttpClientUtil.doGet(url);
            System.err.println(result);
            return result;
        } catch (Exception e) {
            log.error("{}获取access_token失败{}", new Date(), result);
            return null;
        }
    }

    /**
     * 提交成绩
     *
     * @return
     */
    public String submitGrade(CqSport cqSport, String accessToken) {
        String signatures = CipherUtil.generatePassword(CqKey.issuerId + CqKey.SecretKey + accessToken);
        String signaturess = signatures.toUpperCase();
        cqSport.setSignature(signaturess);
        System.out.println(JSONObject.toJSONString(cqSport));
        String data = JSONObject.toJSONString(cqSport);
        String url = SUBMIT_GRADE;
        String result = "";
        try {
            result = HttpClientUtil.doPost(url, data);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("{}上传成绩失败{}", new Date(), result);
            return null;
        }
    }
}
